package com.monochromebytes.mononet.virtual;

import com.monochromebytes.mononet.packet.Inbox;
import com.monochromebytes.mononet.packet.IncomingPacket;

/**
 * A simple implementation of an SimpleInbox.
 * 
 * This implementation of an Inbox will accept every received packet and returns
 * them in that order they were received.
 * 
 * @author ted
 */
public class ForwardingInbox implements Inbox {

    /**
     * The socket that handles the connections.
     */
    private final ConnectionSocket   socket;

    /**TODO
     * Creates a new empty SimpleInbox with a maximum size of 1000.
     */
    public ForwardingInbox(ConnectionSocket socket) {
        this.socket = socket;
    }

    /**TODO
     * If there is no space left in the inbox to receive the new packet the
     * first added packet will be deleted.
     * 
     * @param packet
     *            The received packet.
     */
    @Override
    public void receive(IncomingPacket packet) {
        IncomingDataPacket dataPacket = new IncomingDataPacket(packet);
        VirtualAddress address = dataPacket.getConnectionAddress();
        Connection connection = this.socket.getConnection(address);
        QueuedInbox inbox = connection.getInbox();
        inbox.receive(dataPacket);
    }
}
